﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine.UI;

public class ZJHDealer : MonoBehaviour {

	// Use this for initialization
	public static bool Licening = false;
	private int index = 0;
	private int index2 = 0;
	private int max = 24;
	private GameObject prefabs = null;
	private List<ZjhPlayer> players;
	private 

	void Awake () {
		Licening = false;
		transform.localPosition = new Vector3 (10000, 0, 0);
		prefabs = GameObject.Find ("CardsPre");
	}

	public void StartDealer(List<ZjhPlayer> list,thirteen.ResReadyOverMsg readyover){
		
		transform.localPosition = new Vector3 (30, 0, 0);
		players = new List<ZjhPlayer> ();

		for (int i = 0; i < list.Count; i++) {
			for (int f = 0; f < readyover.players.Count; f++) {
				if( list[i].Id == readyover.players[f]){
					players.Add (list[i]);
				}
			}
		}
		if (players.Count <= 1) {
			return;
		}
		index = 0;
		index2 = 0;
		max = players.Count * 13;
		InvokeRepeating("PlayCard",0.05f,0.05f);

	}

	private void PlayCard(){

		GameObject card =  (GameObject)Instantiate (prefabs,transform);
		card.transform.SetParent(transform);
		card.transform.localPosition = transform.localPosition;

		if( players [index2].Id != info.UserInfo.UserData.id ){
			card.transform.localScale = new Vector3 (0.7f, 0.7f, 0.7f);
		}
		Debug.Log ("index2:" + index2);
		card.GetComponent<ZJHCards> ().setValue (0);
		players [index2].setCard (card.GetComponent<ZJHCards> ());

		index2++;
		if ( index2 >= players.Count ) {
			 index2 = 0;
		}
		index++;
		if(index >= max){
			CancelInvoke ();
			InvokeRepeating ("LiceningOver" , 1f , 0f );
			transform.localPosition = new Vector3 (10000, 0, 0);
		}
	}

	private void LiceningOver(){
		Licening = true;
	}

	void Update () {
		
	}
}
